# -*- python -*-

Import('env')
import os
import sys
from SconsTests import utils

files = [
	'BootManager.cpp',
	]

libs = [
	'core', 'lzo2', 'discio', 'bdisasm',
	'inputcommon', 'common', 'lua', 'z', 'sfml-network',
	]

if env['HAVE_WX']:
	files += [
		'AboutDolphin.cpp',
		'ARCodeAddEdit.cpp',
		'GeckoCodeDiag.cpp',
		'ConfigMain.cpp',
		'Frame.cpp',
		'FrameAui.cpp',
		'FrameTools.cpp',
		'LuaWindow.cpp',
		'LogWindow.cpp',
		'GameListCtrl.cpp',
		'HotkeyDlg.cpp',
		'InputConfigDiag.cpp',
		'InputConfigDiagBitmaps.cpp',
		'ISOFile.cpp',
		'ISOProperties.cpp',
		'PatchAddEdit.cpp',
		'CheatsWindow.cpp',
		'Main.cpp',
		'MemcardManager.cpp',
		'MemoryCards/GCMemcard.cpp',
		'MemoryCards/WiiSaveCrypted.cpp',
		'NetPlay.cpp',
		'NetPlayClient.cpp',
		'NetPlayServer.cpp',
		'NetWindow.cpp',
		'UDPConfigDiag.cpp',
		'WiimoteConfigDiag.cpp',
		'WxUtils.cpp',
		]

	wxlibs = [ 'debwx', 'debugger_ui_util', 'inputuicommon' ]
else:
	files += [
		'MainNoGUI.cpp',
		'cmdline.c',
		]

if sys.platform == 'win32':
	files += [ "stdafx.cpp" ]
elif sys.platform == 'darwin' and not env['HAVE_WX']:
	files += [ 'cocoaApp.m' ]
	exeNoGUI = '#' + env['prefix'] + '/DolphinNoGUI'
elif sys.platform == 'darwin' and env['HAVE_WX']:
	wxlibs += env['wxconfiglibs']
	exeGUI = '#' + env['prefix'] + '/Dolphin.app/Contents/MacOS/Dolphin'

	env.Install('#' + env['prefix'] + '/Dolphin.app/Contents/' +
		'Library/Frameworks/Cg.framework',
		'#Externals/Cg/Cg.framework/Cg')

	env.Install(env['data_dir'], '#Data/Sys')
	env.Install(env['data_dir'], '#Data/User')
	env.Install(env['data_dir'],
		'#Source/Core/DolphinWX/resources/Dolphin.icns')

	mo_files = Glob('#Data/Languages/*/dolphin-emu.mo', strings=True)
	for mo in mo_files:
		index_lo = mo.find("Languages/") + len("Languages/")
		index_hi = mo.find("/dolphin-emu.mo")
		lang = mo[index_lo:index_hi]
		lproj = env['data_dir'] + '/' + lang + ".lproj/dolphin-emu.mo"
		env.InstallAs(lproj, mo)

	from plistlib import writePlist
	def createPlist(target, source, env):
		for srcNode in source:
			writePlist(srcNode.value, str(target[0]))
	env.Append(BUILDERS = {'Plist' : Builder(action = createPlist)})
	env.Plist('#' + env['prefix'] + '/Dolphin.app/Contents/Info.plist',
		Value(dict(
		CFBundleExecutable = 'Dolphin',
		CFBundleIconFile = 'Dolphin.icns',
		CFBundleIdentifier = 'com.dolphin-emu.dolphin',
		CFBundlePackageType = 'APPL',
		CFBundleShortVersionString =
			utils.GenerateRevFile('', Dir('#None').abspath, None),
		CFBundleVersion = '3.0',
		LSMinimumSystemVersion = '10.5.4',
		LSRequiresCarbon = True,
		)))
else:
	files += [ 'X11Utils.cpp' ]
	libs += [ 'SDL' ]
	exeGUI = env['binary_dir'] + '/dolphin-emu'
	exeNoGUI = env['binary_dir'] + '/dolphin-emu-nogui'
	env.InstallAs(env['data_dir'] + '/sys', '#Data/Sys')
	env.InstallAs(env['data_dir'] + '/user', '#Data/User')

if env['HAVE_WX']:
	env.Program(exeGUI, files, LIBS = wxlibs + libs + env['LIBS'])
else:
	env.Program(exeNoGUI, files, LIBS = libs + env['LIBS'])
